//
//  GXPublicStorage.m
//  GaoXunTravel
//
//  Created by 杜梅 on 16/6/5.
//  Copyright © 2016年 May. All rights reserved.
//

#import "GXPublicStorage.h"

#define _MIN_ 60

@interface GXPublicStorage()
{
    NSTimer * timers;
    UIButton *getVirfircationButton;
    
}

@end
@implementation GXPublicStorage

static GXPublicStorage*newInstance;
static dispatch_once_t  predicate;

+ (instancetype)newInstance
{
    dispatch_once(&predicate, ^{
        newInstance = [[GXPublicStorage alloc]init];
    });
    return newInstance;
}

- (BOOL)archiverDataWithObject:(id)object
{
    return [NSKeyedArchiver archiveRootObject:object toFile:[self stateFile]];
}

- (id)unArchiverDataWithKey
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self stateFile]];
}

- (NSString*)stateFile
{
    NSString *dataPath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"MeMeDaiSteward.iAPP.state"];
    return dataPath;
}

- (NSString*)getCacheDirectory
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString * filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"image"];
    
    return filePath;
    
}

- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    
    NSArray * array = [imageName split:@"/"];
    NSString * needStr = [array lastObject];
    //    NSLog(@"--------------lastObject----------%@",needStr);
    
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:needStr];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:YES];
}

- (void)manageVerificationButton:(UIButton *)button{
    if (timers) {
        [timers invalidate];
        timers = nil;
    }
    
    getVirfircationButton = button;
    [getVirfircationButton setEnabled:NO];
    _currentTimer = _MIN_;
    timers = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
    
}



- (void)handleTimer:(NSTimer *)timer
{
    _currentTimer -= 1;
    if (_currentTimer <= 0) {
//        getVirfircationButton.backgroundColor = RGB(136, 197, 6, 1);
        
        [getVirfircationButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [getVirfircationButton setEnabled:YES];
        [timers invalidate];
        
        timers = nil;
        return;
    }
    
    [getVirfircationButton setTitle:[NSString stringWithFormat:@"%ld秒后再次发送",(long)_currentTimer] forState:UIControlStateNormal];
}

@end
